/**
 * input
*/
export const inputSchema = {
    label: {
        label: '表单名',
        widget: 'input',
        ui: {
            placeholder: '请输入'
        }
    },
    span: {
        label: '布局',
        widget: 'inputNumber',
        ui: {
            min: 1,
            max: 24,
            step: 1,
            precision: 0
        }
    },
    defaultValue: {
        label: '默认值',
        widget: 'input',
        ui: {
            placeholder: '请输入'
        }
    },
    size: {
        label: '大小',
        widget: 'select',
        enum: [
            {label: 'medium', value: 'medium'},
            {label: 'small', value: 'small'},
            {label: 'mini', value: 'mini'},
        ],
        ui: {
            placeholder: '请选择'
        }
    },
    placeholder: {
        label: '占位符',
        widget: 'input',
        ui: {
            placeholder: '请输入'
        }
    },
    disabled: {
        label: '禁用',
        widget: 'switch'
    },
    minlength: {
        label: '最小长度',
        widget: 'input',
        ui: {
            placeholder: '请输入'
        }
    },
    maxlength: {
        label: '最大长度',
        widget: 'input',
        ui: {
            placeholder: '请输入'
        }
    },
    clearable: {
        label: '可清空',
        widget: 'switch'
    },
    showSordLimit: {
        label: '输入字数统计',
        widget: 'switch'
    },
    showPassword: {
        label: '切换密码图标',
        widget: 'switch'
    },
    readonly: {
        label: '只读',
        widget: 'switch'
    },
    rows: {
        label: '输入框行数',
        widget: 'inputNumber',
        ui: {
            min: 1,
            max: 999,
            step: 1,
            precision: 0
        }
    },
    autocomplete: {
        label: '自动补全',
        widget: 'select',
        enum: [
            {label: 'on', value: 'on'},
            {label: 'off', value: 'off'},
        ],
        ui: {
            placeholder: '请选择'
        }
    },
    min: {
        label: '最小值',
        widget: 'input',
        props: {
            modifier: 'number'
        },
        ui: {
            placeholder: '请输入'
        }
    },
    max: {
        label: '最大值',
        widget: 'input',
        props: {
            modifier: 'number'
        },
        ui: {
            placeholder: '请输入'
        }
    },
    resize: {
        label: '是否被缩放',
        widget: 'select',
        enum: [
            {label: 'none', value: 'none'},
            {label: 'both', value: 'both'},
            {label: 'horizontal', value: 'horizontal'},
            {label: 'vertical', value: 'vertical'},
        ],
        ui: {
            placeholder: '请选择'
        }
    },
    autofocus: {
        label: '自动获取焦点',
        widget: 'switch'
    }
}

export const createSchema = function(widget) {
    let schema = {
        properties: {}
    }
    switch (widget) {
        case 'input':
            schema = {
                properties: inputSchema
            }
            break;
        default:
            break;
    }

    return schema
}

const baseKey = ['label', 'labelWidth', 'required', 'rules', 'error', 'showMessage', 'inlineMessage', 'size', 'model', 'widget', 'enum', 'defaultValue'];
const colKey = ['span', 'offset', 'push', 'pull', 'pull', 'xs', 'sm', 'md', 'lg', 'xl'];
const propsKey = ['modifier', 'visibleAsync', 'terminateLoad', 'tip', 'pictureCardSize', 'asterisk'];
export const objToProperty = function(widget, formData = {}) {
    let pro = {
        widget: widget
    };
    let arr = [];
    switch(widget) {
        case 'input':
            arr = Object.keys(inputSchema).map(e => ({key: e, value: formData[e]}))
            arr.forEach(v => {
                if(v.value !== undefined && v.value !== null && v.value !== '') {
                    if(baseKey.indexOf(v.key) !== -1) {
                        pro[v.key] = v.value
                    } else if(colKey.indexOf(v.key) !== -1) {
                        if(!pro.col) {
                            pro.col = {}
                        }
                        pro.col[v.key] = v.value
                    } else if(propsKey.indexOf(v.key) !== -1) {
                        if(!pro.props) {
                            pro.props = {}
                        }
                        pro.props[v.key] = v.value
                    } else {
                        if(!pro.ui) {
                            pro.ui = {}
                        }
                        pro.ui[v.key] = v.value
                    }
                }
            })
            break;
    }
    return pro
}